<template>
  <div>
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="100px"
      style="width: 500px"
    >
      <el-form-item label="用户名" prop="uname">
        <el-input v-model="form.uname" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item prop="user" label="账号">
        <el-input v-model="form.user" />
      </el-form-item>
      <el-form-item prop="pwd" label="密码">
        <el-input v-model="form.pwd" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit"> 立即修改 </el-button>
        <el-button @click="$router.push(`/home/user-list`)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        phone: '',
        pwd: '',
        uname: '',
        user: '',
        userid: '',
      },
      rules: {
        // 整理表单验证规则
        phone: [{ required: true, message: '必填', trigger: 'blur' }],
        pwd: [{ required: true, message: '必填', trigger: 'blur' }],
        uname: [{ required: true, message: '必填', trigger: 'blur' }],
        user: [{ required: true, message: '必填', trigger: 'blur' }],
      },
    }
  },

  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$http.userApi.change(this.form).then(res => {
            console.log(res)
            if (res.data.code == 200) {
              this.$message({ type: 'success', message: '修改成功' })
              this.$router.go(-1)
            }
          })
        }
      })
    },
  },

  mounted() {
    let id = this.$route.params.userid
    this.$http.userApi.query({ userid: id }).then(res => {
      console.log(res)
      this.form = res.data.data[0]
    })
  },
}
</script>

<style lang="scss" scoped></style>
